package cn.rubintry.cardtest;

import static com.app.tool.BytesUtil.hexChar2Byte;

public class ScanUtils {
    public static byte[] hexString2ByteArray(String hexStr) {
        if(hexStr == null) {
            return null;
        } else {
            if(hexStr.length() % 2 != 0) {
                hexStr = hexStr + "0";
            }

            byte[] data = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                char hc = hexStr.charAt(2 * i);
                char lc = hexStr.charAt(2 * i + 1);
                byte hb = hexChar2Byte(hc);
                byte lb = hexChar2Byte(lc);
                if(hb < 0 || lb < 0) {
                    return null;
                }

                int n = hb << 4;
                data[i] = (byte)(n + lb);
            }

            return data;
        }
    }

    public static char[] getChars(byte[] bytes)
    {
        char[] chars = new char[bytes.length];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = ((char)(0xFF & bytes[i]));
        }
        return chars;
    }


}
